﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.manager
{
    public partial class operatoredit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Request["userid"]))
            {
                throw new Base.CustomException("缺少参数userid");
            }

            Hashtable edituser = PhoneBook.Users.Users.get(Request["userid"]);
            if (edituser == null)
            {
                throw new Base.CustomException("参数userid不正确");
            }

            if (!IsPostBack)
            {

                txb_username.Text = Commons.StrUtil.FormatValue(edituser["username"]);
                txb_nickname.Text = Commons.StrUtil.FormatValue(edituser["nickname"]);

                DataSet roles = PhoneBook.Manage.SysRole.getManagerRole();
                Commons.ControlUtil.BindDDL(ddl_roleid, roles, "rolename", "roleid");

                Hashtable sysuserrole = PhoneBook.Manage.SysUserRole.getuserrole(Request["userid"]);
                if (sysuserrole != null &&
                ddl_roleid.Items.FindByValue(Commons.StrUtil.FormatValue(sysuserrole["roleid"])) != null)
                    ddl_roleid.SelectedValue = Commons.StrUtil.FormatValue(sysuserrole["roleid"]);
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            if (!txb_userpwd.Text.Equals(txb_userpwd2.Text))
            {
                Helper.ShowMessage(u_message, controls.UCMessageType.attention, "两次的密码不一致");
                return;
            }
            Hashtable newuser = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Request["userid"]));
            addLog(newuser);
            newuser["username"] = txb_username.Text;
            Dx.ZcEncrypt.Encrypt3DES encrypt = new Dx.ZcEncrypt.Encrypt3DES();

            if (txb_userpwd.Text != string.Empty)
                newuser["userpwd"] = txb_userpwd.Text;//encrypt.EncryptString(Dx.ZcEncrypt.Encrypt.GetMD5(txb_userpwd.Text));

            newuser["nickname"] = txb_nickname.Text;

            PhoneBook.Users.Users.edit(newuser);

            if (!string.IsNullOrWhiteSpace(ddl_roleid.SelectedValue))
            {
                string userid = PhoneBook.Users.Users.getByName(newuser["username"].ToString())["userid"].ToString();
                string roleid = ddl_roleid.SelectedValue;
                PhoneBook.Manage.SysUserRole.add(userid, roleid);
            }
            Helper.ShowMessage(u_message, controls.UCMessageType.success, "保存成功");
        }

        //添加日志
        private void addLog(Hashtable user)
        {
            string content = string.Empty;

            Dx.ZcEncrypt.Encrypt3DES encrypt = new Dx.ZcEncrypt.Encrypt3DES();

            if (user["username"].ToString() != txb_username.Text)
                content += string.Format("名称:{0}->{1};", user["username"], txb_username.Text);

            if (txb_userpwd.Text != string.Empty)
            {
                if (user["userpwd"].ToString() != encrypt.EncryptString(Dx.ZcEncrypt.Encrypt.GetMD5(txb_userpwd.Text)))
                    //content += string.Format("密码:{0}->{1};", user["userpwd"], encrypt.EncryptString(Dx.ZcEncrypt.Encrypt.GetMD5(txb_userpwd.Text)));
                    content += string.Format("密码变更;");
            }

            if (user["nickname"].ToString() != txb_nickname.Text)
                content += string.Format("昵称:{0}->{1};", user["nickname"], txb_nickname.Text);

            if (!string.IsNullOrEmpty(content))
            {
                content = string.Format("修改了管理员信息({0})。{1}", user["username"], content);

                AddLog(content);
            }
        }
    }
}